package com.ladybug.flow.common.exception.config;

import com.ladybug.flow.common.exception.customexceptions.ErrorPageException;
import com.ladybug.flow.common.exception.customexceptions.ServiceException;
import com.ladybug.flow.common.exception.enums.ErrorEnum;
import com.ladybug.flow.common.vo.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
* @description: TODO
* @author long
* @date 2022/10/17 17:19
* @version 1.0
*/
@Slf4j
@RestControllerAdvice
public class ExceptionHandlerConfig {
    /**
     * 业务异常 统一处理
     */
    @ExceptionHandler(value = ServiceException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public ResponseResult exceptionHandler400(ServiceException e){
        return ResponseResult.error(e.getErrorEnum());
    }

    /**
     * 错误页面异常 统一处理
     */
    @ExceptionHandler(value = ErrorPageException.class)
    @ResponseBody
    public ResponseResult exceptionHandler(ErrorPageException e){
        return ResponseResult.error(e.getErrorEnum());
    }
    /**
     * 空指针异常 统一处理
     */
    @ExceptionHandler(value =NullPointerException.class)
    @ResponseBody
    public ResponseResult exceptionHandler500(NullPointerException e){
        return ResponseResult.error(ErrorEnum.INTERNAL_SERVER_ERROR);
    }

    /**
     * 未知异常 统一处理
     */
    @ExceptionHandler(value =Exception.class)
    @ResponseBody
    public ResponseResult exceptionHandler(Exception e){
        return ResponseResult.error(ErrorEnum.UNKNOWN);
    }

    @ExceptionHandler(value = BadCredentialsException.class)
    @ResponseBody
    public ResponseResult exceptionHandler401(Exception e){
        return ResponseResult.error(ErrorEnum.USER_NAME_OR_PWD_ERROR);
    }

}
